﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Threading;
using System.Globalization;

namespace WcfContrib.Exceptions
{
    /// <summary>
    /// Represents a base class for fault exceptions
    /// </summary>
    [Serializable]
    public class FaultExceptionBase : FaultException
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FaultExceptionBase"/> class.
        /// </summary>
        public FaultExceptionBase()
            : this(string.Empty)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FaultExceptionBase"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        public FaultExceptionBase(string message)
            : base(new FaultReason(new FaultReasonText(message, Thread.CurrentThread.CurrentUICulture)))
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FaultExceptionBase"/> class.
        /// </summary>
        /// <param name="name">The fault name.</param>
        /// <param name="message">The message.</param>
        public FaultExceptionBase(string name, string message)
            : this(name, Constants.FaultsNamespace, message)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FaultExceptionBase"/> class.
        /// </summary>
        /// <param name="name">The fault name.</param>
        /// <param name="ns">The namespace.</param>
        /// <param name="message">The message.</param>
        public FaultExceptionBase(string name, string ns, string message)
            : base(new FaultReason(new FaultReasonText(message, Thread.CurrentThread.CurrentUICulture)),
                    FaultCode.CreateSenderFaultCode(name, ns))
        {
        }

        /// <summary>
        /// Creates a fault representing denied access.
        /// </summary>
        /// <returns>The fault</returns>
        public static Exception CreateAccessDeniedFault()
        {
            FaultCode code = FaultCode.CreateSenderFaultCode(
              "FailedAuthentication",
              "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

            FaultReasonText faultText =
              new FaultReasonText("Access is Denied.", CultureInfo.CurrentCulture);

            return new FaultException(new FaultReason(faultText), code);
        }
    }
}
